<?php

class db{
	
	public function __construct(){
		require ROOT.'/config.php';
		mysql_connect($dbhost, $dbuser, $dbpw);
		mysql_select_db($dbname);
		$dbuser = $dbpw = $pconnect = $sdb = NULL;
	}
	
	public function select($sql){
		$res = mysql_query($sql);
		$rtn = array();
		if(!empty($res)){
			while ($result = mysql_fetch_array($res)){
				$rtn[]=$result;
			}
		}
		return $rtn;
	}
	
	public function one($sql){
		$res = mysql_query($sql);
		$array = @mysql_fetch_array($res);
		if(empty($array)){
			return false;
		}
		return $array;
	}
	
	public function insert($array,$table){
		foreach ($array as $key=>$val) {
			$keys[] = '`'.$key.'`';
			$vals[] = '"'.$val.'"';
		}
		$k = implode(",",$keys);
		$v = implode(",",$vals);
		$sql = "INSERT INTO `$table` ($k) VALUES ($v)";
		mysql_query($sql);
		if (!mysql_insert_id()){
			return false;
		}else{
			return true;
		}
	}
	
	public function update($array,$table,$where){
		foreach ($array as $key=>$val) {
			$sets[] = '`'.$key.'` = "'.$val.'"';
		}
		$set = implode(",",$sets);
		$sql = "UPDATE `$table` SET $set WHERE $where";
		$res=mysql_query($sql);
		if (!mysql_affected_rows()){
			return false;
		}else{
			return true;
		}
	}
	
	private function insert_id(){
		return ($id = mysql_insert_id($this->curlink)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}
	
	public function delete($column,$id,$table){
		$sql = "DELETE FROM $table WHERE `$column` = '$id'";
		$res = mysql_query($sql);
		if (!mysql_affected_rows($res)){
			return false;
		}else{
			return true;
		}
	}
	
	public function table($table){
		return "pre_".$table;
	}
}
?>